package com.sduept.zj103.model;

import com.sduept.tool.TimeTool;

import java.nio.charset.Charset;
import java.util.Date;

public class Apdu{
    public Apci apci;
    public Asdu asdu;

    public Apdu() {
        apci = new Apci();
        asdu = new Asdu();
    }

    public int getElemetLen() {
        return apci.asduLen - Asdu.ASDUHEADERLEN;
    }

    public void encodeAsdu13(int asduAddr, int rii, int cot, String fileName, int beginPosition) { //召唤录波文件
        apci.frameType = FrameType.I;
        apci.asduLen = 52;
        asdu.type = 0x0D;
        asdu.vsq = 0x81;
        asdu.cot = cot; //0x14一般命令，0x18稳态录波文件
        asdu.asduAddr = asduAddr;
        asdu.fun = 0xFF;
        asdu.inf = 0;
        asdu.element = new byte[apci.asduLen - Asdu.ASDUHEADERLEN];
        asdu.element[0] = (byte) rii;
        byte[] fileNameBytes = fileName.getBytes(Charset.forName("utf-8"));
        for(int i = 0; i < Math.min(fileNameBytes.length, 40); i++) {
            asdu.element[1 + i] = fileNameBytes[i];
        }
        asdu.element[41] = (byte)(beginPosition & 0x00FF);
        asdu.element[42] = (byte)((beginPosition >> 8) & 0x00FF);
        asdu.element[43] = (byte)((beginPosition >> 16) & 0x00FF);
        asdu.element[44] = (byte)((beginPosition >> 24) & 0x00FF);

        FileInfo fileInfo = new FileInfo();
        fileInfo.fileName = fileName;
    }

    public void encodeAsdu15(int asduAddr, int rii, int cot, Date begin, Date end) { //召唤文件列表
        apci.frameType = FrameType.I;
        apci.asduLen = 22;
        asdu.type = 0x0F;
        asdu.vsq = 0x81;
        asdu.cot = cot; //0x09所有文件列表，0x14有故障发生的录波文件列表，0x18稳态录波文件列表
        asdu.asduAddr = asduAddr;
        asdu.fun = 0xFF;
        asdu.inf = 0;
        asdu.element = new byte[apci.asduLen - Asdu.ASDUHEADERLEN];
        asdu.element[0] = (byte) rii;
        byte[] startByte = TimeTool.getCp56Time2a(begin);
        byte[] endByte = TimeTool.getCp56Time2a(end);
        for(int i = 0; i < startByte.length; i++) {
            asdu.element[1 + i] = startByte[i];
        }
        for(int i = 0; i < startByte.length; i++) {
            asdu.element[8 + i] = endByte[i];
        }
    }

    public void encodeAsdu17(int asduAddr, int rii, int cot, Date begin, Date end) { //召唤事件列表
        apci.frameType = FrameType.I;
        apci.asduLen = 22;
        asdu.type = 0x11;
        asdu.vsq = 0x81;
        asdu.cot = cot; //0x09时上送指定时间段内所有的事件文件列表， 0x14时只上送判断为有故障发生的事件列表
        asdu.asduAddr = asduAddr;
        asdu.fun = 0xFF;
        asdu.inf = 0;
        asdu.element = new byte[apci.asduLen - Asdu.ASDUHEADERLEN];
        asdu.element[0] = (byte) rii;
        byte[] startByte = TimeTool.getCp56Time2a(begin);
        byte[] endByte = TimeTool.getCp56Time2a(end);
        for(int i = 0; i < startByte.length; i++) {
            asdu.element[1 + i] = startByte[i];
        }
        for(int i = 0; i < startByte.length; i++) {
            asdu.element[8 + i] = endByte[i];
        }
    }

    public void encodeAsdu20(int asduAddr, int inf, int rii) { //读装置时间命令
        apci.frameType = FrameType.I;
        apci.asduLen = 9;
        asdu.type = 0x14;
        asdu.vsq = 0x81;
        asdu.cot = 0x14;
        asdu.asduAddr = asduAddr;
        asdu.fun = 0xFF;
        asdu.inf = inf;
        asdu.element = new byte[apci.asduLen - Asdu.ASDUHEADERLEN];
        asdu.element[0] = (byte) rii;
        asdu.element[1] = 1;
    }

    public void encodeAsdu113(int asduAddr, int rii, String fileName, int beginPosition) { //召唤录波简报文件
        apci.frameType = FrameType.I;
        apci.asduLen = 52;
        asdu.type = 0x71;
        asdu.vsq = 0x81;
        asdu.cot = 0x14;
        asdu.asduAddr = asduAddr;
        asdu.fun = 0xFF;
        asdu.inf = 0;
        asdu.element = new byte[apci.asduLen - Asdu.ASDUHEADERLEN];
        asdu.element[0] = (byte) rii;
        byte[] fileNameBytes = fileName.getBytes(Charset.forName("utf-8"));
        for(int i = 0; i < Math.min(fileNameBytes.length, 40); i++) {
            asdu.element[1 + i] = fileNameBytes[i];
        }
        asdu.element[41] = (byte)(beginPosition & 0x00FF);
        asdu.element[42] = (byte)((beginPosition >> 8) & 0x00FF);
        asdu.element[43] = (byte)((beginPosition >> 16) & 0x00FF);
        asdu.element[44] = (byte)((beginPosition >> 24) & 0x00FF);
    }

}
